#include <stdio.h>
#include "ofc/DFile.h"

int main(int argc, char *argv[])
{
  DFile *fil1  = [DFile alloc];
  DFile *fil2  = [DFile new  ];
  DText *str;
                                       // Init with a file
  [fil1 init :"test.txt" :"w"];

  if ([fil1 isOpen])
  {
    printf("File \"test.txt\" is open for writing.\n");

    if ([fil1 writeLine :"This is a test file for the DFile example."])
      printf("Text succesfully writen in file.\n");
    else
      printf("Text could not be written in file:%d\n", [fil1 error]);
    
    [fil1 close];
  }
  else
    printf("File \"test.txt\" could not be opened:%d.\n", [fil1 error]);

  if ([fil2 open :"test.txt" :"r"])
  {
    printf("File \"test.txt\" is open for reading.\n");

                                       // Read the contents of the text file
    str = [fil2 readLine];
    while (str != nil)
    {
      printf("Read text:%s\n", [str cstring]);
      [str free];
      str = [fil2 readLine];
    }

    [fil2 close];                      // Close the file
  }
  else
    printf("File \"test.txt\" could not be opened:%d.\n", [fil2 error]);

  [fil1 free];                         // Cleanup
  [fil2 free];

                                      // Class methods for files
  if ([DFile move :"test.txt" :"test2.txt"])
    printf("File \"test.txt\" succesfully renamed to \"test2.txt\".\n");
  else
    printf("File \"test.txt\" could not be renamed:%d.\n", [DFile error]);

  printf("File \"test2.txt\" is %s a directory.\n", ([DFile isDirectory :"test2.txt"] ? "" : "not"));

  printf("Size of file \"test2.txt\":%lld\n", [DFile size :"test2.txt"]);

  if ([DFile remove :"test2.txt"])
    printf("File \"test2.txt\" is succesfully removed.\n");
  else
    printf("File \"test2.txt\" could not be removed:%d\n", [DFile error]);

  return 0;
}

